package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.entity.activity.fivehero.config.FiveHeroTitleConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;
import java.util.Map;


@Cmd(code = GameProtocol.S_FIVEHERO_APPOINT_FROM_CROSS, desc = "")
public class CrossFiveHeroAppointPotionCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        FiveHeroProto.CrossFiveHeroWinnerAppointSyncMsg reqMsg = FiveHeroProto.CrossFiveHeroWinnerAppointSyncMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        if(reqMsg.getSendReward()){
            FiveHeroConfig config = FiveHeroMgr.getConfig();
            Map<Integer, FiveHeroTitleConfig> configMap = config.getTitleConfigMap();
            FiveHeroTitleConfig titleConfig = configMap.get(reqMsg.getPosition());
            if(!titleConfig.getRewards().equals("0") && !StringUtils.isNullOrEmpty(titleConfig.getRewards())){
                UserInfo userInfo = UserMgr.getUserInfo(userId);
                String activityName = ActivityMgr.getActivityName(config.getActivityInfo(), userInfo.getLanguage());
                String positionName = ServerLanguageMgr.getContent(titleConfig.getName(), userInfo.getLanguage());
                String title,content;
                title = ServerLanguageMgr.getContent(config.getTitleMailTitle(), userInfo.getLanguage());
                content = StringUtils.format(ServerLanguageMgr.getContent(config.getTitleMailContent(), userInfo.getLanguage()),
                        activityName, positionName);
                Property reward = PropertyHelper.parseNewStringToProperty(titleConfig.getRewards());
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
                FiveHeroMgr.getLogger().error("玩家被任命{},奖励{}",titleConfig.getCampId(),PropertyHelper.parsePropertyToString(reward));
            }
        }
    }

}
